﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HiStaff.Util;
using HiStaff.Base;
using HiStaff.Control;
using HiStaff.iSystem;
using HiStaff.Domain.CO;
using HiStaff.Util.Enum;
namespace HiStaff.iTime
{
    public partial class IORegister : BaseRegister
    {
        public IORegister()
        {
            InitializeComponent();
        }
        protected override bool FormLoad()
        {
            base.CreateCommandBar(CommandBarManager.ActionCommand.New,
                                      CommandBarManager.ActionCommand.Separator,
                                      CommandBarManager.ActionCommand.Save,
                                      CommandBarManager.ActionCommand.Cancel,
                                      CommandBarManager.ActionCommand.Separator,
                                      CommandBarManager.ActionCommand.Exit);
            empSelect.EmployeeSelected = ListEmployeeRegist;

            cboSign.DisplayMember = "DISPLAYNAME";
            cboSign.ValueMember = "CODE";

            var lstSign = HiStaff.iSystem.Data.ALLCOSIGN.FindAll(obj => obj.STATUS == 1 &&
                obj.GROUPID == (decimal?)WorkSignGroup.IOSign &&
                obj.DATAMODEID == (decimal?)WorkSignMode.Regist);
            cboSign.SetDataBinding(lstSign, "CODE");
            txtDate.Value = DateTime.Today;
            txtTime.Value = DateTime.Now;

            return base.FormLoad();
        }
        protected override bool CheckValidation(System.Windows.Forms.Control container)
        {
            if (!base.CheckValidation(container))
                return false;
            else
            {
                CO_SUM_SEARCH objSearch;
                foreach (var item in empSelect.EmployeeSelected)
                {
                    objSearch = new CO_SUM_SEARCH();
                    objSearch.EMPLOYEEID = item.ID;
                    objSearch.STARTDATE = txtDate.Value;
                    objSearch.ENDDATE = txtDate.Value;
                    Service.ExcuteStoreNonQuery("PKG_CALCOLEX", "PRS_CHKCOSTATUS", objSearch);

                    if (objSearch.CLOSEPERIOD > 0)
                    {
                        MessageBox.Show(GetMessageById(Constant.MSG_INVALID_PERIOD), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return false;
                    }
                }
                return true;
            }
        }
        protected override bool SaveButtonClick()
        {
            CO_SIGN_LST objSign;
            CO_DAYRGT objUpdate;

            foreach (var item in empSelect.EmployeeSelected)
            {
                objSign = (CO_SIGN_LST)cboSign.SelectedItem;
                objUpdate = new CO_DAYRGT();
                objUpdate.EMPLOYEEID = item.ID;
                objUpdate.CODE = objSign.CODE;
                objUpdate.WORKINGDAY = txtDate.Value;
                objUpdate.NOTE = txtNote.Text.Trim();

                switch ((WorkSignDataType)objSign.DATATYPEID)
                {
                    case WorkSignDataType.Number:
                        objUpdate.NVALUE = null;
                        break;

                    case WorkSignDataType.String:
                        objUpdate.SVALUE = txtTime.Text;
                        break;

                    case WorkSignDataType.DateTime:
                        objUpdate.DVALUE = txtTime.Value;
                        break;

                    default:
                        break;
                }

                Service.ExcuteStoreNonQuery("PRI_CO_DAYRGT", objUpdate);
                if (!string.IsNullOrEmpty(objUpdate.ERRCODE))
                {
                    MessageBox.Show(objUpdate.ERRMSG, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;
                }
            }

            MessageBox.Show(GetMessageById(Constant.MSG_RGT_SUCCESS), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);

            ResetInputControl(this);

            this.RegisterResult = DialogResult.OK;

            return true;
        }
    }
}
